package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;
import java.util.List;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    /*private DataSource dataSource;
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }*/

    public void save(Account account) {
        accountDao.save(account);
    }

    public void update(Account account) {
        accountDao.update(account);
    }

    public void delete(int id) {
        accountDao.delete(id);
    }

    public List<Account> findAll() {
        return accountDao.findAll();
    }

    public Account findById(int id) {
        return accountDao.findById(id);
    }


    public void transfer(String outName, String inName, Double money) {
        accountDao.updateMoneyByName(outName,-money);
        System.out.println(1/0);
        accountDao.updateMoneyByName(inName,money);

       /* // 开启事务
        DataSourceTransactionManager ds = new DataSourceTransactionManager(dataSource);
        // 事务定义
        TransactionDefinition de = new DefaultTransactionDefinition();
        // 事务状态
        TransactionStatus st = ds.getTransaction(de);

        accountDao.updateMoneyByName(outName,-money);
//        System.out.println(1/0);
        accountDao.updateMoneyByName(inName,money);

        ds.commit(st);*/
    }
}
